package org.ss.psci.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.ss.psci.entity.PmDoc;
import org.ss.psci.entity.PmStorage;

/**
 * Data access object (DAO) for domain model class PmDoc.
 * 
 * @see org.ss.psci.entity.PmDoc
 * @author MyEclipse Persistence Tools
 */

public class PmDocDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(PmDocDAO.class);
	// property constants
	public static final String FILEPATH = "filepath";
	public static final String NAME = "name";
	public static final String REMARK = "remark";
	
	public static final String PROJECTID = "projectId";

	public List findByProject(Object name) {
		return findByProperty(PROJECTID, name);
	}
	
	public PmDoc copyEntity(PmDoc dest, PmDoc orig) {
		if (dest == null || orig == null) {
			return new PmDoc();
		}
		if (orig.getName() != null) {
			dest.setName(orig.getName());
		}
		if (orig.getFilepath() != null) {
			dest.setFilepath(orig.getFilepath());
		}

		if (orig.getRemark() != null) {
			dest.setRemark(orig.getRemark());
		}
		if (orig.getSize() != null) {
			dest.setSize(orig.getSize());
		}
		if (orig.getCreator() != null) {
			dest.setCreator(orig.getCreator());
		}
		return dest;
	}
	protected void initDao() {
		// do nothing
	}

	public void save(PmDoc transientInstance) {
		log.debug("saving PmDoc instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(PmDoc persistentInstance) {
		log.debug("deleting PmDoc instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public PmDoc findById(java.lang.Integer id) {
		log.debug("getting PmDoc instance with id: " + id);
		try {
			PmDoc instance = (PmDoc) getHibernateTemplate().get(
					"org.ss.psci.entity.PmDoc", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(PmDoc instance) {
		log.debug("finding PmDoc instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding PmDoc instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from PmDoc as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByFilepath(Object filepath) {
		return findByProperty(FILEPATH, filepath);
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByRemark(Object remark) {
		return findByProperty(REMARK, remark);
	}

	public List findAll() {
		log.debug("finding all PmDoc instances");
		try {
			String queryString = "from PmDoc";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public PmDoc merge(PmDoc detachedInstance) {
		log.debug("merging PmDoc instance");
		try {
			PmDoc result = (PmDoc) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(PmDoc instance) {
		log.debug("attaching dirty PmDoc instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(PmDoc instance) {
		log.debug("attaching clean PmDoc instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static PmDocDAO getFromApplicationContext(ApplicationContext ctx) {
		return (PmDocDAO) ctx.getBean("PmDocDAO");
	}
}